/*
 *  Copyright 2019-2023 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.base.DeleteReq;
import me.zhengjie.modules.system.domain.AddressDetail;
import me.zhengjie.modules.system.domain.WxUser;
import me.zhengjie.modules.system.domain.req.AddressReq;
import me.zhengjie.modules.system.domain.vo.AddressDetailQueryCriteria;
import me.zhengjie.modules.system.mapper.WxUserMapper;
import me.zhengjie.modules.system.service.AddressDetailService;
import me.zhengjie.modules.system.service.WxUserService;
import me.zhengjie.modules.system.domain.vo.WxUserQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
 * @author yiwei.he
 * @date 2024-03-22
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "微信用户（学生）")
@RequestMapping("/api/wxUser")
@Slf4j
public class WxUserController {

    private final WxUserService wxUserService;

    private final AddressDetailService addressDetailService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check()")
    public void exportWxUser(HttpServletResponse response, WxUserQueryCriteria criteria) throws IOException {
        log.info("/api/wxUser/download  导出微信用户（学生）数据：{}",criteria);
        wxUserService.download(wxUserService.queryAll(criteria), response);
    }

    @PostMapping
    @Log("查询微信用户（学生）")
    @ApiOperation("查询微信用户（学生）")
    public ResponseEntity<WxUser> queryWxUser(@RequestBody WxUserQueryCriteria criteria){
        log.info("/api/wxUser  查询微信用户（学生）:{}",criteria);
        WxUser wxUser = wxUserService.selectById(criteria);
        return new ResponseEntity<>(wxUser,HttpStatus.OK);
    }

    @PostMapping("/update")
    @Log("修改微信用户信息（学生）")
    @ApiOperation("修改微信用户信息（学生）")
    public ResponseEntity<Object> updateWxUser(@Validated @RequestBody WxUser resources) throws BizException {
        log.info("/api/wxUser/update  修改微信用户信息（学生）:{}",resources);
        wxUserService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping(value = "/getUserList")
    @Log("查询微信用户（学生）")
    @ApiOperation("查询微信用户（学生）")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<PageResult<WxUser>> queryWxUser(WxUserQueryCriteria criteria, Page<Object> page){
        log.info("/api/wxUser/getUserList  查询微信用户(后管)：{}",criteria);
        return new ResponseEntity<>(wxUserService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping(value = "/add")
    @Log("新增微信用户（学生）")
    @ApiOperation("新增微信用户（学生）")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> createWxUser(@Validated @RequestBody WxUser resources){
        log.info("/api/wxUser/getUserList  新增微信用户(后管)：{}",resources);
        wxUserService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @DeleteMapping
    @Log("删除微信用户（学生）")
    @ApiOperation("删除微信用户（学生）")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> deleteWxUser(@RequestBody List<Integer> ids) {
        log.info("/api/wxUser  删除微信用户(后管)：{}",ids);
        wxUserService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/saveOrUpdateAddress")
    @Log("修改学生用户地址")
    @ApiOperation("修改学生用户地址")
    public ResponseEntity<Object> updateWxUser(@Validated @RequestBody AddressDetailQueryCriteria req){
        log.info("/api/wxUser/saveOrUpdateAddress  修改学生用户地址：{}",req);
        return new ResponseEntity<>(wxUserService.updateAddress(req),HttpStatus.OK);
    }

    @GetMapping("/addressList")
    @Log("查询地址管理（微信小程序个人模块）")
    @ApiOperation("查询地址管理（微信小程序个人模块）")
    public ResponseEntity<PageResult<AddressDetail>> queryAddressDetail(AddressDetailQueryCriteria criteria,Page<Object> page) throws BizException {
        log.info("/api/wxUser/addressList  查询地址管理：{},分页参数：{}",criteria, page);
        return new ResponseEntity<>(wxUserService.addressList(criteria,page),HttpStatus.OK);
    }

    @DeleteMapping("/address")
    @Log("删除地址管理（微信小程序个人模块）")
    @ApiOperation("删除地址管理（微信小程序个人模块）")
    public ResponseEntity<Boolean> deleteAddressDetail(@RequestBody DeleteReq req) {
        log.info("/api/wxUser/address  删除地址管理：{}",req);
        return new ResponseEntity<>(addressDetailService.deleteAll(req.getIds()),HttpStatus.OK);
    }

}